package com.hyny.framework.platform.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.hyny.framework.platform.system.entity.dto.DataPowerTypeDTO;
import com.hyny.framework.platform.system.entity.pojo.SysRegion;
import com.hyny.framework.platform.system.entity.vo.DataPowerTypeDataVO;
import com.hyny.framework.platform.system.entity.vo.DataPowerTypeVO;
import com.hyny.framework.platform.system.enums.DataPowerTypeEnum;
import com.hyny.framework.platform.system.mapper.SysDataPowerMapper;
import com.hyny.framework.platform.system.entity.pojo.SysDataPower;
import com.hyny.framework.platform.system.service.SysDataPowerService;
import com.hyny.framework.platform.system.service.SysRegionService;
import com.hyny.framework.platform.system.utils.enums.ChildOperateTypeEnum;
import com.hyny.framework.base.enums.YesOrNoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据权限(SysDataPower)表服务实现类
 *
 * @author 张崇阳
 * @since 2024-02-26 08:58:17
 */
@Service("sysDataPowerService")
public class SysDataPowerServiceImpl extends MPJBaseServiceImpl<SysDataPowerMapper, SysDataPower> implements SysDataPowerService {
    @Autowired
    private SysRegionService sysRegionService;

    @Override
    public List<DataPowerTypeVO> dataPowerTypeList() {
        List<DataPowerTypeVO> list = new ArrayList<>();
        //项目字段
        list.add(new DataPowerTypeVO()
                .setFieldName(DataPowerTypeEnum.ADD.getFieldName())
                .setFieldTitle(DataPowerTypeEnum.ADD.getFieldTitle())
                .setChildOperateType(ChildOperateTypeEnum.SELECT)
                .setChildFieldName(DataPowerTypeEnum.PROVINCE_CODE.getFieldName())
                .setStructureName(DataPowerTypeEnum.ADD.getFieldTitle())

        );
        return list;
    }

    @Override
    public DataPowerTypeVO dataPowerChild(DataPowerTypeDTO dto) {

        switch (DataPowerTypeEnum.getEnum(dto.getChildFieldName())) {
            case PROVINCE_CODE:
                return getProvinceCode();
            case CITY_CODE:
                return getCityCode(dto);
            case DISTRICT_CODE:
                return getDistrictCode(dto);

        }
        return null;
    }

    /**
     * 获取省份信息
     *
     * @return
     */
    public DataPowerTypeVO getProvinceCode() {
        DataPowerTypeVO dataPowerTypeVO = new DataPowerTypeVO()
                .setFieldName(DataPowerTypeEnum.PROVINCE_CODE.getFieldName())//当前字段名
                .setFieldTitle(DataPowerTypeEnum.PROVINCE_CODE.getFieldTitle())//当前字段标题
                .setChildOperateType(ChildOperateTypeEnum.SELECT)//查询类型 下级位下拉列表
                .setChildFieldName(DataPowerTypeEnum.CITY_CODE.getFieldName())//下级字段名
                .setIsOptional(YesOrNoEnum.YES)//本级是否可选
                .setStructureName(DataPowerTypeEnum.PROVINCE_CODE.getFieldTitle())//拼接结构名称
                .setDataPowerTypeDataVOList(new ArrayList<DataPowerTypeDataVO>())//初始化数据值集合
                ;
        List<SysRegion> regions = sysRegionService.list(new LambdaQueryWrapper<SysRegion>().eq(SysRegion::getLevelId, "1"));

        for (SysRegion region : regions
        ) {
            dataPowerTypeVO.getDataPowerTypeDataVOList().add(
                    new DataPowerTypeDataVO(String.valueOf(region.getId()), region.getName())
            );
        }

        return dataPowerTypeVO;
    }

    /**
     * 获取城市信息
     *
     * @return
     */
    public DataPowerTypeVO getCityCode(DataPowerTypeDTO dto) {
        DataPowerTypeVO dataPowerTypeVO = new DataPowerTypeVO()
                .setFieldName(DataPowerTypeEnum.CITY_CODE.getFieldName())//当前字段名
                .setFieldTitle(DataPowerTypeEnum.CITY_CODE.getFieldTitle())//当前字段标题
                .setChildOperateType(ChildOperateTypeEnum.SELECT)//查询类型 下级位下拉列表
                .setChildFieldName(DataPowerTypeEnum.DISTRICT_CODE.getFieldName())//下级字段名
                .setIsOptional(YesOrNoEnum.YES)//本级是否可选
                .setStructureName(structureName(dto.getStructureName(), DataPowerTypeEnum.CITY_CODE.getFieldTitle()))//拼接结构名称
                .setDataPowerTypeDataVOList(new ArrayList<DataPowerTypeDataVO>())//初始化数据值集合
                ;
        List<SysRegion> regions = sysRegionService.list(new LambdaQueryWrapper<SysRegion>()
                .eq(SysRegion::getLevelId, "2")
                .eq(SysRegion::getParentId, dto.getFieldDataValue())
        );

        for (SysRegion region : regions
        ) {
            dataPowerTypeVO.getDataPowerTypeDataVOList().add(
                    new DataPowerTypeDataVO(String.valueOf(region.getId()), region.getName())
            );
        }
        return dataPowerTypeVO;
    }

    /**
     * 获取区县信息
     *
     * @return
     */
    public DataPowerTypeVO getDistrictCode(DataPowerTypeDTO dto) {
        DataPowerTypeVO dataPowerTypeVO = new DataPowerTypeVO()
                .setFieldName(DataPowerTypeEnum.DISTRICT_CODE.getFieldName()) //当前字段名
                .setFieldTitle(DataPowerTypeEnum.DISTRICT_CODE.getFieldTitle())//当前字段标题
                .setChildOperateType(ChildOperateTypeEnum.END_OF)//查询类型 因为是结尾了所以标识END_OF
                .setIsOptional(YesOrNoEnum.YES)//本级是否可选
                .setStructureName(structureName(dto.getStructureName(), DataPowerTypeEnum.DISTRICT_CODE.getFieldTitle()))//拼接结构名称
                .setDataPowerTypeDataVOList(new ArrayList<>());
        List<SysRegion> regions = sysRegionService.list(new LambdaQueryWrapper<SysRegion>()
                .eq(SysRegion::getLevelId, "3")
                .eq(SysRegion::getParentId, dto.getFieldDataValue())
        );

        for (SysRegion region : regions
        ) {
            dataPowerTypeVO.getDataPowerTypeDataVOList().add(
                    new DataPowerTypeDataVO(String.valueOf(region.getId()), region.getName())
            );
        }
        return dataPowerTypeVO;
    }


    /**
     * 拼接机构名
     *
     * @param originalName
     * @param addName
     * @return
     */
    public String structureName(String originalName, String addName) {
        return originalName + ">" + addName;
    }




}

